package hapiserverproject;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

//VS4E -- DO NOT REMOVE THIS LINE!
public class Arayuz extends JFrame {

	private static final long serialVersionUID = 1L;
	private JButton startButton;
	private JButton stopButton;
	private JSpinner jSpinner0;
	private JLabel portText;
	private JLabel statusLabel;
	private static JTextArea jTextArea0;
	private static JScrollPane jScrollPane0;


	public static Sunucu sunucu = new Sunucu();
	
	private static JLabel jLabel0;

	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
	public Arayuz() {
		initComponents();
	}

	private void initComponents() {
		setLayout(new GroupLayout());
		add(getstartButton(), new Constraints(new Leading(12, 12, 12), new Leading(12, 12, 12)));
		add(getstopButton(), new Constraints(new Leading(107, 10, 10), new Leading(12, 12, 12)));
		add(getJSpinner0(), new Constraints(new Leading(284, 57, 10, 10), new Leading(13, 12, 12)));
		add(getportText(), new Constraints(new Leading(240, 10, 10), new Leading(15, 10, 10)));
		add(getJLabel0(), new Constraints(new Leading(627, 10, 10), new Leading(17, 38, 38)));
		add(getJScrollPane0(), new Constraints(new Leading(12, 674, 10, 10), new Leading(53, 359, 10, 10)));
		setSize(703, 427);
	}

	private JScrollPane getJScrollPane0() {
		if (jScrollPane0 == null) {
			jScrollPane0 = new JScrollPane();
			jScrollPane0.setViewportView(getjTextArea0());
		}
		return jScrollPane0;
	}

	private JTextArea getjTextArea0() {
		if (jTextArea0 == null) {
			jTextArea0 = new JTextArea();
		}
		return jTextArea0;
	}

	private JLabel getJLabel0() {
		if (jLabel0 == null) {
			jLabel0 = new JLabel();
			jLabel0.setText("jLabel0");
		}
		return jLabel0;
	}

	private JLabel getportText() {
		if (portText == null) {
			portText = new JLabel();
			portText.setText("Port:");
		}
		return portText;
	}

	private JSpinner getJSpinner0() {
		if (jSpinner0 == null) {
			jSpinner0 = new JSpinner();
//			jSpinner0.
			
		}
		return jSpinner0;
	}
	
	public boolean checkStatus()
	{
		return sunucu.checkStatus();
	}

	private JButton getstopButton() {
		if (stopButton == null) {
			stopButton = new JButton();
			stopButton.setText("Stop");
			stopButton.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					stopButtonActionActionPerformed(event);
				}
			});
		}
		return stopButton;
	}

	private JButton getstartButton() {
		if (startButton == null) {
			startButton = new JButton();
			startButton.setText("Start");
			startButton.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					startButtonActionActionPerformed(event);
				}
			});
		}
		return startButton;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class.
	 * Note: This class is only created so that you can easily preview the result at runtime.
	 * It is not expected to be managed by the designer.
	 * You can modify it as you like.
	 */
	public static void main(String[] args) {

		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				Arayuz frame = new Arayuz();
				frame.setDefaultCloseOperation(Arayuz.EXIT_ON_CLOSE);
				frame.setTitle("Arayuz");
				frame.getContentPane().setPreferredSize(frame.getSize());
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
				frame.pack();
				isRunningStatusCheck();
				sunucu.receiveText(jTextArea0);

			}
		});
	}

	private void startButtonActionActionPerformed(ActionEvent event) {
		sunucu.startServer();
		startButton.setEnabled(false);
		stopButton.setEnabled(true);
	}

	private void stopButtonActionActionPerformed(ActionEvent event) {
		sunucu.stopServer();	
		startButton.setEnabled(true);
		stopButton.setEnabled(false);
	}
	
	public static void isRunningStatusCheck() {
		int delay = 500; // delay for 0.5 sec.
		int period = 1000; // repeat every sec.
		Timer timer = new Timer();

		timer.scheduleAtFixedRate(new TimerTask() {
			public void run() {
				sunucu.statusUpdate();
				jLabel0.setText(sunucu.getStatusLabelString());
			}
		}, delay, period);

	}
	
	public Object getStatusLabel() {
		// TODO Auto-generated method stub
		return statusLabel;
	}
}
